const { db, query } = require("../../db/db");
const util = require("util");

async function GetArticleAndAuthorInfoDetails(req, res) {
  let UserId = req.body.UserId;
  let ArticleId = req.body.ArticleId;
  const select_articleInfo_sql = util.format(
    "Select \
    ArticleId,\
    PublisherId,\
    IFNULL(ArticleTitle,'') as ArticleTitle, \
    IFNULL(Abstract,'') as Abstract, \
    IFNULL(ArticleContent,'') as ArticleContent, \
    IFNULL(ArticleContent,'') as ArticleContent, \
    Collect,\
    LikeNum,\
    PageView,\
    IFNULL(ArticleImgUrl,'') as ArticleImgUrl, \
    IFNULL(ArticleTabs,'') as ArticleTabs, \
    IFNULL(PublishTime,'') as PublishTime \
    From Article where ArticleId=%s",
    ArticleId
  );

  const select_articleInfo_res = await query(select_articleInfo_sql);

  if (select_articleInfo_res[0].PublisherId) {
    const select_AuthorInfo_sql = util.format(
      'Select IFNULL(UserName,"") as UserName,\
      IFNULL(HeaderImg,"") as HeaderImg,\
      IFNULL(SelfDescription,"") as SelfDescription,\
      Follow,\
      Fans,ArticleNum from User Where UserId=%s',
      select_articleInfo_res[0].PublisherId
    );

    const select_authorInfo_res = await query(select_AuthorInfo_sql);

    return res.send({
      status: 0,
      ArticleInfo: select_articleInfo_res[0],
      AuthorInfo: select_authorInfo_res[0],
    });
  }

  res.send({
    status: 0,
  });
}

module.exports = { GetArticleAndAuthorInfoDetails };
